Skip to content

fix(transport): finish a stdio frame whose send future is dropped - #1558

Open
joulook wants to merge 1 commit into
modelcontextprotocol:mainfrom
joulook:fix/progress-flush
Open

joulook wants to merge 1 commit into
modelcontextprotocol:mainfrom
joulook:fix/progress-flush

Conversation

@joulook

@joulook joulook commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1557.

Motivation and Context

This regression was introduced by #1549, our pull request. Since #1549, the service loop drops a progress
notification's send future when the peer cancels the request.
AsyncRwTransport::send buffers the whole frame in its FramedWrite, then flushes. If the future is dropped while
that flush waits on a full pipe, the tail stays buffered. close() drops the writer without flushing it, so with no
later message the peer reads a truncated line before EOF.

This was pointed out by the automated review on #1549. It also breaks the Transport::send contract that #1549
documented.

How Has This Been Tested?

tests/test_progress_flush.rs: a 64-byte output pipe and a progress frame longer than the pipe, whose write is pending
when the request is cancelled; in the forced-EOF case no cancel is sent, and the drain deadline drops the write after
EOF. The first four check that every line the peer reads is a whole JSON message; the fifth checks
that shutdown stays bounded.

test main 50fc08a this PR
frame abandoned mid-flush, then EOF fails 3/3 (output ends inside the progress frame, at byte 64) passes 3/3
the same with a BufWriter holding the tail below an empty codec buffer fails 3/3 passes 3/3
cancel and EOF sent together fails 3/3 passes 3/3
EOF read before the send future is dropped (order asserted) fails 3/3 passes 3/3
a peer that stops reading: the server still stops in time passes passes (stops after CLOSE_FLUSH_TIMEOUT)

Against the rejected receive()-side flush (below), the BufWriter, cancel-with-EOF and forced-EOF tests fail 3/3,
and the timing test fails 2/3 (its stalled reads).

On 181cb98, before #1549, the first test passes: progress sends were never abandoned.

Full runs on this branch:

  • cargo test -p rmcp --features <all but local>: 1 993 / 0 / 18;
  • --all-features: 1 305 / 0 / 32;
  • --lib --no-default-features: 210 / 0 / 0;
  • clippy with -D warnings: clean in all 34 runs (all features, all but local, each feature alone, no default
    features, default).

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Additional context

  • The change. AsyncRwTransport::close() now flushes the writer before dropping it: FramedWrite::flush, which
    also flushes the AsyncWrite below the codec. The flush is bounded by a 500 ms timeout (CLOSE_FLUSH_TIMEOUT).
    • A frame whose send was abandoned mid-flush is completed at close, whatever the order of EOF and the drop of the
      send future. This is best effort: within the 500 ms and absent a write error. A peer that does not read in that
      time still gets a partial line.
    • Before close, the next outgoing message completes it, as before.
    • send() and receive() are unchanged, so the change adds no output-dependent wait to receive(). Its existing
      protocol-error reply path already waits for the writer.
    • If bytes are left, closing takes up to 500 ms longer. This includes the child-process transport, whose
      graceful_shutdown() closes the transport, and so the child's stdin, before it waits for the child to exit.
  • Rejected alternatives.
    • Finishing the flush in a spawned task that holds the writer. A peer that stops reading then keeps that task, and
      close(), alive past the drain deadline, and blocked_sends_respect_drain_deadline fails.
    • Finishing the flush in receive() while the writer is free. It misses a frame whose send is dropped after EOF has
      been read, and one still buffered in a writer below the codec. It also stops reads while the peer does not read.
  • Scope. This changes AsyncRwTransport only, the stdio and child-process transport.
    • SinkStreamTransport has the same SinkExt::send shape and a no-op close(). Whether a dropped send leaves a
      partial message there depends on the user's Sink; a FramedWrite would.
    • The worker-based transports pass whole messages through a channel.
  • Conformance model. The conformance model of the first fix wrote a message as one step. A byte-level
    refinement of the transport model (each frame written in two chunks, which a full pipe can hold apart; Daavar benchmarks/mcp-cancel, the byte-level frame
    cell) shows the truncated frame as a violation on main and none on this branch, over all interleavings of that model
    within the cell. The model assumes a peer that keeps reading drains within the close bound and that no write fails;
    its drain covers only a write already holding the writer (the cell's README states these limits).

Found with. We are a research team at MPI-SWS building Daavar, a conformance checker for distributed protocol
implementations. This follow-up came from the automated review comment on #1549; we confirmed it natively (above) and with
a byte-level refinement of our model of rmcp's stdio write path, where each frame is written in two chunks that a full pipe
can hold apart. On that model the transport at main violates the specification program and the fixed transport conforms, over
all interleavings of the cell. This fix was prepared with AI assistance and reviewed by us.

Reproduce with Daavar (conformance test)

The cell ca,s0,frA: one tool call with a progress token, the client cancels it and shuts down, then reads the server's output
to EOF. The specification program writes both chunks of a frame together, so every frame the client reads is whole or absent.
implfo follows rmcp main 50fc08a; implff follows the fix. The join prints the words of the implementation the specification
does not produce as uncovered.

git clone --recurse-submodules https://github.com/joulook/daavar.git && cd daavar
git checkout 7fb70be && git submodule update --init --recursive
(cd traceforge && cargo build --release -p traceforge --bin conf-join)
cd benchmarks/mcp-cancel/rust && cargo build --release
B=target/release/tf_mcp_cancel; J=../../../traceforge/target/release/conf-join
$B export spec --out spec.jsonl  --model fifo -- spec,ca,s0,frA
$B export impl --out main.jsonl  --model fifo -- implfo,ca,s0,frA; $J --decide spec.jsonl main.jsonl   # violation: 1 uncovered of 3, Head(progress) then Eof
$B export impl --out fixed.jsonl --model fifo -- implff,ca,s0,frA; $J --decide spec.jsonl fixed.jsonl  # 0 uncovered

The cells frC (no cancel; the drain deadline drops the held write after EOF) and frE (a later request after the abandoned
one) are in the benchmark's README with their counts.

Since modelcontextprotocol#1549 the service loop drops the send future of a progress
notification once the peer cancels its request. AsyncRwTransport::send
encodes the whole frame into the FramedWrite buffer and then flushes it;
dropped during that flush (a full output pipe), the rest of the frame
stays buffered, in the codec or in a buffered writer below it. A later
message flushes it, but close() dropped the writer unflushed, so with
nothing sent after it the peer read a truncated JSON line before EOF.

close() now flushes the writer (FramedWrite::flush, which also flushes
the writer below the codec) before dropping it. This is best effort,
bounded by CLOSE_FLUSH_TIMEOUT (500 ms) so that a peer that stopped
reading cannot hold the shutdown; a write error or the timeout still
leaves the line partial. send() and receive() are unchanged.

Tests: the abandoned frame reaches the peer whole over a plain pipe, over
a BufWriter holding the tail below an empty codec buffer, when EOF
arrives together with the cancel, and when EOF is read before the send
future is dropped (each fails on main); with a peer that stops reading,
the server still stops within the drain deadline plus
CLOSE_FLUSH_TIMEOUT.
@joulook
joulook requested a review from a team as a code owner December 3, 2026 11:23
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T11:27:59.595267Z 15435aa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdio: a progress frame abandoned mid-flush is left partial on the wire (follow-up to #1548/#1549)

1 participant